UserScriptをbundleするDeno script
べつにコピーしてこなくても直接実行できたぽい
使い方
以下を実行する
code:sh
例
code:sh
code
bundle対象(entryFilePathが指すscrapbox上のscript.js)の中身を更新しても反映されない気がするyosider.icon
cacheが使われている?
~/.cache/deno/deps/https/scrapbox.ioを削除してみたりしたが変わらず
scrapboxのページ名を変えれば反映される
~/cache/deps/https/scrapbox.ioを削除する
~/.cacheではないので注意
実行ファイルを更新したときは-rをつけると更新を反映できる
code:build.ts
import { run } from './script.ts';
const {_: entryFilePath, importMap, external, ...rest} = parse(Deno.args); if (typeof entryFilePath === 'number') throw Error('entryFilePath must be string');
if (importMap) {
if (/^https?:\/\//.test(importMap)) {
const res = await fetch(importMap);
imports = await res.json();
} else {
imports = JSON.parse(await Deno.readTextFile(importMap));
}
}
await run(entryFilePath, imports, {external, ...rest});
update 'cache'
code:mod.ts
//prettier-ignore
type Loader = 'js' | 'jsx' | 'ts' | 'tsx' | 'css' | 'json' | 'text' | 'base64' | 'file' | 'dataurl' | 'binary' | 'default';
interface Config {
directory: string
}
export function cache({ importmap = { imports: {} }, directory }: Config): Plugin {
Cache.configure({ directory })
return {
name: 'deno-cache',
setup(build) {
build.onResolve({ filter: /.*/ }, async (args) => {
if (build.initialOptions.external?.includes(args.path)) return {external: true};
const resolvedPath = resolve(args.path, importmap)
if (resolvedPath.startsWith('http')) {
return {
path: resolvedPath,
namespace: 'deno-cache',
}
}
if (args.namespace === 'deno-cache') {
return {
path: new URL(resolvedPath, args.importer).toString(),
namespace: 'deno-cache',
}
}
return { path: join(args.resolveDir, resolvedPath) }
})
build.onLoad({ filter: /.*/, namespace: 'deno-cache' }, async (args) => {
const file = await Cache.cache(args.path, undefined, 'deps')
const contents = await Deno.readTextFile(file.path)
const ext = file.meta.url.split('.').pop() as Loader
const loader = ext.match(/"j|tsx?$/) ? ext : 'js'
return { contents, loader }
})
},
}
}
code:script.ts
import { cache } from './mod.ts';
// 特定のpropertyを削るやつ
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export async function run(filename: string, imports: {key: string: string}, {external, ...rest }: Omit<BuildOptions, 'entryPoints' | 'platform' | 'plugins'>) { let useTempFile = false;
let result: BuildResult | undefined = undefined;
try {
if (/^https?:\/\//.test(filename)) {
const tempname = index-${Math.random()}.ts;
await Deno.writeTextFile(tempname, import '${filename}';export * from '${filename}';);
filename = tempname;
useTempFile = true;
}
const options: BuildOptions = {
platform: 'neutral',
external: Array.isArray(external) ? external : (external ? external : []), ...rest
};
result = await build(options);
stop();
} catch(e) {
throw e;
}finally {
//後始末
if (useTempFile) await Deno.remove(filename);
if (await exists('./cache')) await Deno.remove('./cache', { recursive: true });
}
return result;
}
/icons/hr.icon
const {code} =をコメントアウト
externalを指定しなかったせい?
未検証
消し忘れとのこと
生成したコードの書き込み先(--outfile)を指定しない場合のみcodeが戻り値に生える
code:txt
error: TS2339 ERROR: Property 'code' does not exist on type 'BuildResult'. const {code} =
~~~~